package com.lbh.cloudgallery.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbh.cloudgallery.entity.MemberEntity;
import com.lbh.cloudgallery.entity.vo.MemberLoginVo;
import com.lbh.cloudgallery.entity.vo.MemberRegistVo;
import com.lbh.cloudgallery.service.MemberService;
import com.xxx.common.utils.JwtUtils;
import com.xxx.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "前端-用户模块")
@RestController
@RequestMapping("front/member")
public class FrontMemberController {

    @Autowired
    private MemberService memberService;

    //登录
    @ApiOperation(value="用户登录")
    @PostMapping("login")
    public R login(@RequestBody MemberLoginVo vo){
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token= memberService.login(vo);
        if(token!=null) {
            return R.ok().setData(token);
        }else{
            return R.error();
        }
    }

    //用户注册
    @ApiOperation(value="用户注册")
    @PostMapping("register")
    public R register(@RequestBody MemberRegistVo vo){
        memberService.register(vo);
        return R.ok();
    }

    //根据token获取用户信息
    @ApiOperation(value="个人信息展示")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法，根据request对象获取头信息，返回用户Username
        String memberUsername=JwtUtils.getMemberByJwtToken(request);
        //查询数据库获取用户信息
        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",memberUsername);
        MemberEntity member=memberService.getOne(wrapper);
        return R.ok().setData(member);
    }
}
